package cn.xdj.blue.java.netty5.simple.client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class HelloClientHandler extends ChannelHandlerAdapter {
	private final ByteBuf firstMsg;

	public HelloClientHandler() {
		byte[] request = "请求访问".getBytes();
		firstMsg = Unpooled.buffer(request.length);
		firstMsg.writeBytes(request);
	}
	@Override
	//@Skip
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("----通道连接----");
		ctx.writeAndFlush(firstMsg);
		ctx.flush();
	}
	@Override
	//@Skip
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		System.out.println("----channelRead----");
		ByteBuf buf = (ByteBuf) msg;
		byte[] response = new byte[buf.readableBytes()];
		buf.readBytes(response);
		String body = new String(response,"UTF-8");
		System.out.println("----s_response:"+body+"----");
	}
}
